package mo.daliy;

import mo.common.ListNode;

import java.util.HashSet;
import java.util.Set;

public class L817 {

    public int numComponents(ListNode head, int[] nums) {
        Set<Integer> numSet = new HashSet<>();
        for (int num : nums) {
            numSet.add(num);
        }
        int res = 0;
        boolean flag = false;
        while (head != null) {
            if (numSet.contains(head.val)) {
                flag = true;
            } else {
                if (flag) {
                    res++;
                    flag = false;
                }
            }
            head = head.next;
        }
        if (flag) {
            res++;
        }
        return res;
    }

}
